package com.meichis.mcsappframework.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.meichis.mcsappframework.R;

/**
 * Created by pei on 2016/10/17.
 */

public class SearchDialog extends Dialog implements View.OnClickListener {
    private EditText et_search;
    private ListView lv_list;
    private TextView tv_search;
    private TextChanged mTextChanged;
    private SearchListener mSearchListener;
    private BaseAdapter mAdapter;

    public interface TextChanged {
        void afterTextChanged(String text);
    }

    public interface SearchListener {
        void search(String text);
    }

    public SearchDialog(Context context, SearchListener searchListener) {
        super(context, R.style.EditTextDialog);
        mSearchListener = searchListener;
    }

    public SearchDialog(Context context, TextChanged textChanged, BaseAdapter adapter) {
        super(context, R.style.EditTextDialog);
        mTextChanged = textChanged;
        mAdapter = adapter;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_search);
        getWindow().setWindowAnimations(R.style.searchAnim);
        lv_list = (ListView) findViewById(R.id.lv_list);
        et_search = (EditText) findViewById(R.id.et_search);
        tv_search = (TextView) findViewById(R.id.tv_search);
        findViewById(R.id.bt_cancel).setOnClickListener(this);
        findViewById(R.id.iv_clear).setOnClickListener(this);
        tv_search.setOnClickListener(this);
        et_search.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
            }
        });
        et_search.requestFocus();
        et_search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String str = s.toString();
                if (str.length() > 0) {
                    findViewById(R.id.iv_clear).setVisibility(View.VISIBLE);
                } else {
                    findViewById(R.id.iv_clear).setVisibility(View.GONE);
                }
                if (mTextChanged != null) {
                    if (str.contains("\n")) {
                        str = str.replaceAll("\n", "");
                        et_search.setText(str);
                    }
                    mTextChanged.afterTextChanged(str);
                }


                if (mSearchListener != null) {
                    if (str.contains("\n")) {
                        str = str.replaceAll("\n", "");
                        mSearchListener.search(str);
                    } else {
                        if (str.length() > 0) {
                            tv_search.setVisibility(View.VISIBLE);
                            tv_search.setText("搜索'" + s.toString() + "'");
                        } else {
                            tv_search.setVisibility(View.GONE);
                        }
                    }
                }
            }
        });
        if (mAdapter != null) {
            lv_list.setVisibility(View.VISIBLE);
            lv_list.setAdapter(mAdapter);
        }
        if (mSearchListener != null) {
            tv_search.setOnClickListener(this);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
        et_search.setText("");
    }

    @Override
    public void onClick(View v) {
        if (v.getId() == R.id.bt_cancel) {
            this.cancel();
        }
        if (v.getId() == R.id.iv_clear) {
            et_search.setText("");
        }

        if (v.getId() == R.id.tv_search) {
            mSearchListener.search(et_search.getText().toString());
            this.cancel();
        }
    }

    @Override
    public void cancel() {
        super.cancel();
    }
}